//
//  ProductFrame.m
//  UI Demo
//
//  Created by Rainy Zou on 15/11/30.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "ProductFrame.h"
#import "Product.h"



#define MARGIN 0

@implementation ProductFrame

-(instancetype)initWithProduct:(Product *)product
{
    if (self == [super init]) {
        CGFloat imageW = 100;
        self.imageF = CGRectMake(MARGIN, MARGIN, imageW, imageW);
        CGFloat nameW  = imageW -MARGIN/2;
        CGSize size = [product.name sizeWithFont:[UIFont systemFontOfSize:FONTSIZE] constrainedToSize:CGSizeMake(nameW, MAXFLOAT)];
        self.nameF = CGRectMake(MARGIN/2, CGRectGetMaxY(self.imageF) + MARGIN, MAX(size.width, nameW), size.height);
        NSString *str =[NSString stringWithFormat:@"价格: ￥%.2lf", product.price];
        size = [str sizeWithFont:[UIFont systemFontOfSize:FONTSIZE] constrainedToSize:CGSizeMake(nameW, MAXFLOAT)];
        self.priceF = CGRectMake(MARGIN/2, CGRectGetMaxY(self.nameF), MAX(size.width, nameW), size.height );
        
        self.productSize = CGSizeMake(CGRectGetMaxX(self.imageF)+MARGIN, CGRectGetMaxY(self.priceF)+MARGIN);
    }
    return  self;
}

+(instancetype)frameByProduct:(Product *)product
{
    return [[self alloc]initWithProduct:product];
}

@end
